<?php

/**
 * Admin
 * Administration
 * @package
 * @author kubicek
 * @copyright Copyright (c) 2010
 * @version $Id$
 * @access public
 */
class Admin extends Plugin
{
	public $plugin_name = "Administration";
	public $plugin_short_desc = "Administration plugin";
	public $plugin_long_desc = "Administration of other plugins and Yaps!";
	public $version = "1.0";
	public $has_sidebar = true;
	public $needs_install = false; //installed with install.php

	private $auth = false;
	private $login_displayed = false;

	/**
	 * Constructor
	 */
	function __construct()
	{
		global $theme,$db;
		$this->db = $db;

		$theme->set_theme('admin');

		$this->check_auth();
	}

	public function start()
	{
		if($this->auth)
		{
			echo 'Welcome to administration of Yaps! Choose something from menu.';
		}
		else
		{
			$this->check_auth();
		}
	}

	/**
	 * Admin::generate_sidebar()
	 * Generate sidebar with functions
	 * @return
	 */
	public function generate_sidebar()
	{
		global $theme, $core;

		if($this->auth)
		{
			$out = '<ul class="sideNav">';

			// is it active link?
			$active = ($core->action == 'generalsettings') ? '&raquo;&nbsp;' : '';
			$out .= '<li><a href="admin-generalsettings">'.$active.'General settings</a></li>';

			$active = ($core->action == 'pluginsinfo') ? '&raquo;&nbsp;' : '';
			$out .= '<li><a href="admin-pluginsinfo">'.$active.'Plugins information</a></li>';

			$active = ($core->action == 'pluginsinfo') ? '&raquo;&nbsp;' : '';
			$out .= '<li><a href="admin-routingtable">'.$active.'Routing table</a></li>';

			// lets loop through plugins settings
			for($i = 0;$i < sizeof($core->plugins);$i++)
			{
				if($core->action == 'pluginsettings' && $core->get_hsc('plugin') == $core->plugins[$i])
				{
					$out .= '<li><a href="admin-pluginsettings?plugin='.$core->plugins[$i].'">&raquo;&nbsp;'.ucfirst($core->plugins[$i]).'</a></li>';
				}
				else
				{
					$out .= '<li><a href="admin-pluginsettings?plugin='.$core->plugins[$i].'">'.ucfirst($core->plugins[$i]).'</a></li>';
				}
			}

			$out .= '<li><a href="admin-logout">Logout</a></li>';
			$out .= '</ul>';

			$theme->assign('sidebar',$out);
		}
		else
		{
			$this->check_auth();
		}
	}

	public function plugins_info()
	{
		global $core;

		if($this->auth)
		{
			$core->plugin_info();
		}
		else
		{
			$this->check_auth();
		}
	}

	/**
	 * Admin::admin_settings()
	 * Mandatory function in every plugin. Display and handle changing of settings for this plugin
	 * @return
	 */
	public function admin_settings()
	{
		if($_POST['password'])
		{
			$res = $this->db->query("UPDATE ".DB_PREFIX."yaps_admin SET password = '".md5($_POST['password'].SALT)."'");
			if($res) echo 'Password updated succesfully!<br />';
		}

		echo '<h3>Change admin password</h3>
					<form action="" method="post">
					<input type="password" name="password" /><input type="submit" value="Change admin password" />
					</form>';
	}

	/**
	 * Admin::plugin_settings()
	 * This function handles settings for every plugin including admin
	 * @return
	 */
	public function plugin_settings()
	{
		global $core;

		if($this->auth)
		{
			$plugin_name = $core->get_hsc('plugin');

			echo '<h1>'.ucfirst($plugin_name).' settings</h1>';

			$plugin = $core->load_plugin($plugin_name);

			$plugin->admin_settings();
		}
		else
		{
			$this->check_auth();
		}
	}

	/**
	 * Admin::general_settings()
	 * Main screen for general settings of Yaps!
	 * @return
	 */
	public function general_settings()
	{
		if($this->auth)
		{
			echo '<h1>General settings</h1><br />';

			$datagrid = new Datagrid(DB_PREFIX.'yaps_config');

			echo '<h3>Yaps_config table</h3>
					<p>Edit only if you know what you are doing, otherwise you can make some serious problems.</p>';
			$datagrid->draw_grid();
			echo '<br />';

			echo '<h3>Yaps_menu table</h3>
					<p>You can edit menu here.</p>';

			$datagrid->set_table(DB_PREFIX.'yaps_menu');
			$datagrid->set_order('position','ASC');
			$datagrid->add_filter('id');
			$datagrid->draw_grid();
		}
		else
		{
			$this->check_auth();
		}
	}

	/**
	 * Admin::routing_table()
	 * Takes care of settings of routing table and routes in Yaps!
	 * @return
	 */
	public function routing_table()
	{
		$grid = new Datagrid(DB_PREFIX.'yaps_routing_table');
		$grid->add_filter('id');
		$grid->draw_grid();
	}

	/**
	 * Admin::check_auth()
	 * Checks authentification of user according to session
	 * @return
	 */
	private function check_auth()
	{
		if($_SESSION['yaps_auth'] == true)
		{
			$this->auth = true;
		}
		else
		{
			$this->login_screen();
		}
	}

	/**
	 * Admin::login_screen()
	 * Creates the login screen for user
	 * @return
	 */
	public function login_screen()
	{
		if(!$this->login_displayed)
		{
			echo '<fieldset>
<legend>Login</legend>
<form action="admin-login" method="post">
<table>
<tr><td>Login</td><td><input type="text" name="username" size="30" /></td></tr>
<tr><td>Password</td><td><input type="password" name="password" size="30" /></td></tr>
<tr><td colspan="2"><input type="submit" value="Login" size="30" /></td></tr>
</table>
</form>
</fieldset>';
			$this->login_displayed = true;
		}
	}

	/**
	 * Admin::login()
	 * Try to login user to administration
	 * @return
	 */
	public function login()
	{
		global $core;

		//get values from POST
		$username = $core->post_hsc('username');
		$password = md5($core->post('password').SALT);

		//ask db
		$query = $this->db->query("SELECT * FROM ".DB_PREFIX."yaps_admin WHERE username = '".$username."' AND password = '".$password."'");
		if($this->db->num_rows($query) > 0)
		{
			$this->auth = true;
			$_SESSION['yaps_auth'] = true;
			$core->refresh('admin');
		}
		else
		{
			$core->err('Sorry, bad login/password! Try it again.');
			$this->login_screen();
		}
	}

	/**
	 * Admin::logout()
	 * Logout user :)
	 * @return
	 */
	public function logout()
	{
		global $core;

		$_SESSION['yaps_auth'] = false;
		$this->auth = false;
		$core->refresh('admin');
	}
}


?>